//
//  MetaService.swift
//  SwiftCode
//
//  Created by yuhus on 2023/8/8.
//

import Alamofire

enum MetaService: URLRequestConvertible {
  case visitorLogin
  case refreshToken
  case query
  case ugcReleased
  case imTokenQuery
  case buddyList
  
  var baseURL: URL {
    return URL(string: "https://api.metaworld.fun")!
  }
  
  var method: HTTPMethod {
    switch self {
    case .imTokenQuery, .buddyList:
      return .get
    default:
      return .post
    }
  }
  
  var path: String {
    switch self {
    case .visitorLogin:
      return "/authorize/v2/visitor/sign"
    case .refreshToken:
      return "/authorize/v2/token/renew"
    case .query:
      return "/user/v2/self"
    case .ugcReleased:
      return "/ugc/content/v2/ts/my/released/search"
    case .imTokenQuery:
      return "/chat/v1/token/take"
    case .buddyList:
      return "/buddy/v2/listing"
    }
  }
  
  var parameters: Parameters? {
    switch self {
    case .ugcReleased:
      return [:]
    default: return nil
    }
  }
  
  func asURLRequest() throws -> URLRequest {
    let url = baseURL.appendingPathComponent(path)
    var request = URLRequest(url: url)
    request.method = method
    
    switch method {
    case .post:
      request = try JSONEncoding.default.encode(request, with: parameters)
    default:
      request = try URLEncoding.default.encode(request, with: parameters)
    }
    return request
  }
}
